<template>
	<view class="">
		<view class="my-list">
			<u-list :height="swiperHeight" @scrolltolower="scrolltolower" v-if="dataList.length > 0">
				<u-list-item v-for="(proIm, proIx) in dataList" :key="proIm.id">
					<view v-if="type === '1'" class="item flex align-center" @click="$u.route('/pages/customer/categoryEdit',{id:proIm.id})">
						<view class="shrink0"><u-avatar :src="proIm.shareImageUrl" default-url="/static/logo.png" size="42" /></view>
						<view class="w-100 ml-20 flex justify-between align-center">
							<view class="">{{proIm.name}}</view>
							<view class="">
								<u-tag text="删除" plain type="warning" @click="onDel(proIx)" color="#e54d42" borderColor="#e54d42"  />
							</view>
						</view>
					</view>
					<view class="item flex align-center" v-if="type ==='2'" @click="onRadio(proIx)">
						<uni-icons
							custom-prefix="iconfont" 
							:type="proIm.isSelected?'icon-checkbox-circular-fill':'icon-checkbox-circular'" 
							:color="proIm.isSelected?'#e54d42':''"
							size="24"
						/>
						<view class="w-100 ml-20 flex justify-between align-center">
							<view class="">{{proIm.name}}</view>
						</view>
					</view>
				</u-list-item>
				<u-loadmore 
					:status="loadmore.status" 
					:icon-type="loadmore.iconType"
					:load-text="loadmore.loadText.loadText" 
					margin-top="20" 
					margin-bottom="20"
					class="coreshop-padding-top-10"
				/>
			</u-list>
		</view>
		<!-- 无数据时默认显示 -->
		<view class="emptybox" v-if="dataList.length == 0">
			<u-empty icon-size="150" text="暂无数据" mode="data"></u-empty>
		</view>
		<!-- <view class="bottom-button bg-white">
			<u-button type="primary" color="#fa436a" text="新增" @click="$u.route('/pages/project/category/edit')"></u-button>
		</view> -->
		<uni-fab
			v-if="type==='1'"
			:popMenu="false" 
			ref="fab" 
			horizontal="right" 
			:pattern="pattern"
			@fabClick="$u.route('pages/customer/categoryEdit')"
		/>
		<view class="bottom-button pt-10 flex justify-end" v-if="type==='2'">
			<view class="width-180">
				<u-button color="#e54d42" text="确认选择" :disabled="!isRadio" @click="upd"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import listHeightMixin from '@/common/computedAltitude.js'
	export default {
		mixins:[listHeightMixin],
		data() {
			return {
				type: '1', // '1'编辑模式 '2选择模式'
				isRadio: false,
				raix: null,
				loadmore: {
					status: 'loadmore',
					iconType: 'flower',
					loadText: {
						loadmore: '轻轻上拉',
						loading: '努力加载中',
						nomore: '实在没有了'
					},
				},
				dataList: [],
				searchData: {
					page: 1,
					limit: 40
				},
				pattern: { buttonColor :'#e54d42' }
			}
		},
		onLoad(opt) {
			if(opt.type){
				this.type = opt.type
			}
			uni.$on('upUserCategoryList',()=>{
				this.searchData.page = 1
				this.dataList = []
				this.getPageList()
			})
			this.getPageList()
		},
		onUnload() {
			uni.$off('upUserCategoryList')
		},
		methods: {
			upd(){
				uni.$emit('user-category', this.dataList[this.raix])
				uni.navigateBack()
			},
			onRadio(ix){
				this.isRadio = true
				this.raix = ix
				this.dataList.map(im=>{
					im.isSelected = false
					return im
				})
				this.dataList[ix].isSelected = true
			},
			onDel(ix) {
				let that = this
				uni.showModal({
					title: '提示',
					content: `确定要删除${that.dataList[ix].name}吗?`,
					success: function(res) {
						if (res.confirm) {
							that.$reqJsonPost('UserCategoryDoDelete', { id: that.dataList[ix].id }).then(res => {
								if (res.code === 0) {
									that.dataList.splice(ix, 1)
								}
							})
						}
					}
				})
			},
			getPageList() {
				this.$reqPost('UserCategoryGetPageList', this.searchData).then(res => {
					const list = res.data.map(im=>{
						im.isSelected = false
						return im
					})
					this.dataList = [...this.dataList, ...list]
					if (res.count > this.dataList.length) {
						this.loadmore.status = 'loadmore'
						this.searchData.page++
					} else {
						// 数据已加载完毕
						this.loadmore.status = 'nomore'
					}
				})
			},
			scrolltolower() {
				if (this.loadmore.status === 'loadmore') {
					this.getPageList()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.img {
		width: 90rpx;
		height: 90rpx;
		border-radius: 50%;
	}
</style>
